<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="wtc.stu.entity.StuEntity" %>
<%@ page import="wtc.stu.dao.StuDaoDepartment" %><%--
  Created by IntelliJ IDEA.
  User: 马可
  Date: 2022/10/28
  Time: 14:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>系别信息处理</title>
</head>
<body>
<%
    try {
%>
<%
    try {
%>
<%--增加系别信息--%>
<%
    //从form表单获取数据
    String xname = new String(request.getParameter("xname").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    String xremark = new String(request.getParameter("xremark").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    int xid = Integer.parseInt(request.getParameter("xid"));
    // 使用set方法将获取到的数据进行存储并封装
    StuEntity department = new StuEntity();
    department.setXid(xid);
    department.setXname(xname);
    department.setXremark(xremark);
    // 调用相应类的addStu方法将封装好的数据作为参数发送到数据库
    StuDaoDepartment daoDepartment = new StuDaoDepartment();
    daoDepartment.addStu(department);
%>
<%--增加成功提示信息--%>
<script type="text/javascript">
    alert("增加成功！");
    // 跳转到指定页面
    window.location.href = "department.jsp";
</script>
<%
} catch (Exception e) {
%>
<%--修改系别信息--%>
<%
    // 从form表单获取数据
    String newXname = new String(request.getParameter("newXname").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    String newXremark = new String(request.getParameter("newXremark").getBytes("ISO8859_1"), StandardCharsets.UTF_8);
    int newXid = Integer.parseInt(request.getParameter("newXid"));
    // 使用set方法将获取到的数据进行存储并封装
    StuEntity departmentUpdate = new StuEntity();
    departmentUpdate.setGid(newXid);
    departmentUpdate.setGname(newXname);
    departmentUpdate.setGremark(newXremark);
    // 调用相应类的addStu方法将封装好的数据作为参数发送到数据库
    StuDaoDepartment daoGradeUpdate = new StuDaoDepartment();
    daoGradeUpdate.updateStu(departmentUpdate);
%>
<%--修改成功提示信息--%>
<script type="text/javascript">
    alert("修改成功！");
    // 跳转到指定页面
    window.location.href = "department.jsp";
</script>
<%
    }
%>
<%
} catch (Exception e) {
%>
<%--删除系别信息--%>
<%
    // 从form表单获取数据
    int delXid = Integer.parseInt(request.getParameter("delXid"));
    // 调用相应类的方法，并将获取到的数据作为参数
    StuDaoDepartment daoDepartmentDel = new StuDaoDepartment();
    daoDepartmentDel.delStu(delXid);
    // 再次通过form表单获取到的数据调用相应类的getOneById方法进行查询，用返回值来进行判断
    StuEntity pd = daoDepartmentDel.getOneById(delXid);
    int c = pd.getXid();
    if (c == 0) {
%>
<%--删除成功提示信息--%>
<script type="text/javascript">
    alert("删除成功！");
    // 跳转到指定页面
    window.location.href = "department.jsp";
</script>
<%
} else {
%>
<%--删除失败提示信息--%>
<script type="text/javascript">
    alert("删除失败！存在外键关联或其他原因！");
    // 跳转到指定页面
    window.location.href = "department.jsp";
</script>
<%
    }
%>
<%
    }
%>
</body>
</html>
